************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 22 (Sleep Survey - Sleep Correlates)
* Last edited: 07 May 2021
************************************************

************************************************	
*0. Initial Setup
************************************************

	clear all
	set more off
	pause on			
			
************************************************	
*1. Prepping data
************************************************

    use "$d/pop_study_dataset.dta", clear


************************************************	
*3. Regression Tables
************************************************

*** Sleep Survey - Sleep Correlates
	eststo clear
	
	preserve
	
	bys pid: keep if _n == 1
	
	* Regress self-report sleep on all controls without pweights - education and employment are excluded
	eststo: xi: reg sr_sleep income_m income_h female age_q* children, vce(robust)
	distinct(pid) if !mi(sr_sleep) & !mi(income_m) & !mi(income_h) & census == 1 & !mi(female) & !mi(age) & !mi(education) & !mi(children)
	estadd scalar pids = r(ndistinct)
	estadd ysumm, mean
	
	* Regress self-report sleep on all controls without pweights 
	eststo: xi: reg sr_sleep income_m income_h female age_q* bef_college college children employment, vce(robust)
	distinct(pid) if !mi(sr_sleep) & !mi(income_m) & !mi(income_h) & census == 1 & !mi(female) & !mi(age) & !mi(education) & !mi(children)
	estadd scalar pids = r(ndistinct)
	estadd ysumm, mean

	restore	
		
	* Regress night sleep on self-reported 
	eststo: xi: reg acti_sleep sr_sleep_q*, vce(robust)
	distinct(pid) if !mi(acti_sleep) & !mi(sr_sleep) 
	estadd scalar pids = r(ndistinct)
	estadd ysumm, mean
	
	* Similar with above but including other controls - education and employment are excluded
	eststo: xi: reg acti_sleep income_m income_h female age_q* children, vce(robust)
	distinct(pid) if !mi(acti_sleep) & !mi(income_m) & !mi(income_h) & !mi(female) & !mi(age) & !mi(education) & !mi(children)
	estadd scalar pids = r(ndistinct)
	estadd ysumm, mean
	
	* Similar with above but including other controls 
	eststo: xi: reg acti_sleep income_m income_h female age_q* bef_college college children employment, vce(robust)
	distinct(pid) if !mi(acti_sleep) & !mi(income_m) & !mi(income_h) & !mi(female) & !mi(age) & !mi(education) & !mi(children)
	estadd scalar pids = r(ndistinct)
	estadd ysumm, mean

	* Regress total sleep on self-reported sleep 
	eststo: xi: reg total_sleep sr_sleep_q*, vce(robust)
	distinct(pid) if !mi(total_sleep) & !mi(sr_sleep) 
	estadd scalar pids = r(ndistinct)
	estadd ysumm, mean
	
	* Similar with above but including other controls - education and employment are excluded
	eststo: xi: reg total_sleep income_m income_h female age_q* children, vce(robust)
	distinct(pid) if !mi(total_sleep) & !mi(income_m) & !mi(income_h) & !mi(female) & !mi(age) & !mi(education) & !mi(children)
	estadd scalar pids = r(ndistinct)
	estadd ysumm, mean
	
	* Similar with above but including other controls
	eststo: xi: reg total_sleep income_m income_h female age_q* bef_college college children employment, vce(robust)
	distinct(pid) if !mi(total_sleep) & !mi(income_m) & !mi(income_h) & !mi(female) & !mi(age) & !mi(education) & !mi(children)
	estadd scalar pids = r(ndistinct)
	estadd ysumm, mean

	* Output table

		#d;
	    esttab using "$oa/Tables/TableA22_ss_ns_correlates.tex", replace booktabs
		    mgroups("\textbf{Self-reported Night Sleep}" "\textbf{Actigraph Night Sleep}" "\textbf{Actigraph 24-Hour Sleep}", pattern(1 0 1 0 0 1 0 0 0) 
		    span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span}))
		    mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)" "PW")
		    se lab stats(ymean N pids, fmt(%9.2f %9.0f %9.0f) labels("Mean" "N" "Participants")) star(* 0.10 ** 0.05 *** 0.01)
		    b(2) varwidth(32) wrap compress nogap nonumbers nonotes
		    order(sr_sleep_q* income_m income_h female age_q* children bef_college college employment)
		    note("\sym{***} \textit{Significant at the 1 percent level}, \sym{**} \textit{Significant at the 5 percent level}, \sym{*} \textit{Significant at the 10 percent level}")
		;#d cr
	
